using ASP;
using HoWave.Common;
using HoWave.Data;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public class Member_JXC_StockOrderSaleSearch : Member, IRequiresSessionState
{
	protected Literal ltrTitle;
	protected TextBox txtSearchBeginDate;
	protected TextBox txtSearchEndDate;
	protected TextBox txtSearchInfoNumber;
	protected Button btnSearch;
	protected Repeater rptList;
	protected Literal ltrPagePanel;
	protected Panel pnlManage;
	protected Literal Literal1;
	protected Label lblStockNumber;
	protected Label lblInputTime;
	protected Label lblInputer;
	protected TextBox txtInputer;
	protected Label lblReceiptEndDate;
	protected TextBox txtReceiptEndDate;
	protected Label lblBuyerName;
	protected Label lblPayTime;
	protected Label lblDeliver_TotalPrice;
	protected Label lblInvoice_TotalPrice;
	protected Label lblMoneyGoods;
	protected Label lblReceiptMoney;
	protected Label lblAreadyReceiptMoney;
	protected Label lblFee;
	protected Label lblRemarks;
	protected TextBox txtRemarks;
	protected Repeater rptItemInfo;
	protected Button btnSave;
	protected Panel pnlDetail;
	private string linkHead = "";
	private string beforeUrl = "StockOrderSaleSearch.aspx?1=1";
	private int infoOrderID = 0;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	public int InfoOrderID
	{
		get
		{
			if (base.Request.QueryString["infoOrderID"] != null)
			{
				this.infoOrderID = Convert.ToInt32(base.Request.QueryString["infoOrderID"].Trim());
			}
			return this.infoOrderID;
		}
		set
		{
			this.infoOrderID = value;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			this.ShowPanelDisplay(base.ActionTabInfo);
		}
	}
	protected void ShowPanelDisplay(string DisplayName)
	{
		if (DisplayName != null)
		{
			if (DisplayName == "checkinfo")
			{
				this.pnlManage.Visible = false;
				this.pnlDetail.Visible = true;
				this.ltrTitle.Text = "详细信息";
				this.txtInputer.Attributes.Add("style", "display:none;");
				this.txtReceiptEndDate.Attributes.Add("style", "display:none;");
				this.txtRemarks.Attributes.Add("style", "display:none;");
				this.lblInputer.Attributes.Add("style", "display:block;");
				this.lblReceiptEndDate.Attributes.Add("style", "display:block;");
				this.lblRemarks.Attributes.Add("style", "display:block;");
				this.btnSave.Attributes.Add("style", "display:none;");
				this.InitPage();
				return;
			}
			if (DisplayName == "editinfo")
			{
				this.pnlManage.Visible = false;
				this.pnlDetail.Visible = true;
				this.ltrTitle.Text = "修改销售出库单";
				this.txtInputer.Attributes.Add("style", "display:block;");
				this.txtReceiptEndDate.Attributes.Add("style", "display:block;");
				this.txtRemarks.Attributes.Add("style", "display:block;");
				this.lblInputer.Attributes.Add("style", "display:none;");
				this.lblReceiptEndDate.Attributes.Add("style", "display:none;");
				this.lblRemarks.Attributes.Add("style", "display:none;");
				this.btnSave.Attributes.Add("style", "display:;");
				this.InitPage();
				return;
			}
		}
		this.pnlManage.Visible = true;
		this.pnlDetail.Visible = false;
		this.BindData();
	}
	protected void InitPage()
	{
		if (this.InfoOrderID > 0)
		{
			string sql = string.Concat(new object[]
			{
				"select o.BuyerName,MoneyGoods,Deliver_TotalPrice,Invoice_TotalPrice,sio.* from HLJXC_StockInfoOrder as sio,HLShop_Order as o where sio.OrderID=o.OrderFormID and o.SalerID=",
				base.user.UserID,
				" and sio.InfoOrderID=",
				this.InfoOrderID
			});
			DataTable dataTable = this.conn.GetDataTable(sql);
			foreach (DataRow dataRow in dataTable.Rows)
			{
				this.lblStockNumber.Text = dataRow["InfoNumber"].ToString();
				this.lblInputTime.Text = dataRow["CreateDate"].ToString();
				this.lblInputer.Text = dataRow["Inputer"].ToString();
				this.lblReceiptEndDate.Text = dataRow["ReceiptEndDate"].ToString();
				this.lblDeliver_TotalPrice.Text = dataRow["Deliver_TotalPrice"].ToString();
				this.lblInvoice_TotalPrice.Text = dataRow["Invoice_TotalPrice"].ToString();
				this.lblMoneyGoods.Text = dataRow["MoneyGoods"].ToString();
				this.lblReceiptMoney.Text = dataRow["ReceiptMoney"].ToString();
				this.lblAreadyReceiptMoney.Text = dataRow["AlreadyReceiptMoney"].ToString();
				this.lblFee.Text = dataRow["Fee"].ToString();
				this.lblBuyerName.Text = dataRow["BuyerName"].ToString();
				this.lblPayTime.Text = dataRow["ReceiptDate"].ToString();
				this.lblRemarks.Text = dataRow["Remark"].ToString();
				if (base.ActionTabInfo == "editinfo")
				{
					this.txtInputer.Text = dataRow["Inputer"].ToString();
					this.txtReceiptEndDate.Text = dataRow["ReceiptEndDate"].ToString();
					this.txtRemarks.Text = dataRow["Remark"].ToString();
				}
			}
			string sql2 = string.Concat(new object[]
			{
				"select ProductID,ProductName,ProductType,Goods_serial,ProductSmallImageUrl,StockDate,Unit,Price,StockCount from View_JXC_StockOrderInfoItemProduct where OwnerID=",
				base.user.UserID,
				" and InfoOrderID=",
				this.InfoOrderID
			});
			DataTable dataTable2 = this.conn.GetDataTable(sql2);
			this.rptItemInfo.DataSource = dataTable2;
			this.rptItemInfo.DataBind();
			if (dataTable2.Rows.Count <= 0)
			{
				Label label = (Label)this.rptItemInfo.Controls[this.rptItemInfo.Controls.Count - 1].FindControl("lblFootExplain");
				label.Text = "<tr><td colspan='7' align='center'>没有符合条件的数据！</td></tr>";
			}
			for (int i = 0; i < this.rptItemInfo.Items.Count; i++)
			{
				HiddenField hiddenField = (HiddenField)this.rptItemInfo.Items[i].FindControl("hfItemPrice");
				HiddenField hiddenField2 = (HiddenField)this.rptItemInfo.Items[i].FindControl("hfItemStockCount");
				Label label2 = (Label)this.rptItemInfo.Items[i].FindControl("lblItemMoney");
				if (hiddenField != null && hiddenField2 != null)
				{
					if (!string.IsNullOrEmpty(hiddenField.Value) && !string.IsNullOrEmpty(hiddenField2.Value))
					{
						decimal d = Convert.ToDecimal(hiddenField.Value);
						int value = Convert.ToInt32(hiddenField2.Value);
						label2.Text = (d * value).ToString();
					}
				}
			}
		}
	}
	protected string GetQuery()
	{
		string text = "";
		string text2 = Fetch.Get("BeginTime");
		if (!string.IsNullOrEmpty(text2))
		{
			this.txtSearchBeginDate.Text = text2;
		}
		else
		{
			text2 = this.txtSearchBeginDate.Text;
		}
		if (!string.IsNullOrEmpty(text2))
		{
			text = text + " and DATEDIFF(DD, ReceiptDate,'" + text2 + "')<=0";
			this.linkHead = this.linkHead + "&BeginTime=" + text2;
		}
		string text3 = Fetch.Get("EndTime");
		if (!string.IsNullOrEmpty(text3))
		{
			this.txtSearchEndDate.Text = text3;
		}
		else
		{
			text3 = this.txtSearchEndDate.Text;
		}
		if (!string.IsNullOrEmpty(text3))
		{
			if (!string.IsNullOrEmpty(text2))
			{
				if (Convert.ToDateTime(text2) > Convert.ToDateTime(text3))
				{
					this.WriteErrMsg("", Language.Get("Common/DateTimeErr"), "");
				}
			}
			text = text + "and DATEDIFF(DD, ReceiptDate,'" + text3 + "')>=0";
			this.linkHead = this.linkHead + "&EndTime=" + text3;
		}
		string text4 = Fetch.Get("InfoNumber");
		if (!string.IsNullOrEmpty(text4))
		{
			this.txtSearchInfoNumber.Text = text4;
		}
		else
		{
			text4 = this.txtSearchInfoNumber.Text;
		}
		if (!string.IsNullOrEmpty(text4))
		{
			text = text + " and InfoNumber like '%" + text4 + "%'";
			this.linkHead = this.linkHead + "&InfoNumber=" + text4;
		}
		return text;
	}
	private string GetQueryByOrder()
	{
		string result = "order by sio.InfoOrderID desc";
		string value = this.txtSearchBeginDate.Text.Trim();
		string value2 = this.txtSearchEndDate.Text.Trim();
		if (!string.IsNullOrEmpty(value) || !string.IsNullOrEmpty(value2))
		{
			result = "order by ReceiptDate desc";
		}
		return result;
	}
	public void BindData()
	{
		string query = this.GetQuery();
		string queryByOrder = this.GetQueryByOrder();
		int num = 0;
		if (Fetch.Get("pageindex") != null)
		{
			num = Convert.ToInt32(Fetch.GetID("pageindex").ToString());
			if (num == 0)
			{
				num = 1;
			}
		}
		string text = " from HLJXC_StockInfoOrder as sio,HLShop_Order as o where sio.OrderID=o.OrderFormID and StockOrderType='outSell' and o.SalerID=" + base.user.UserID + " ";
		int num2 = 10;
		int listCount = BLLInstance.BllClientByPage.GetListCount("select count(*)" + text + query);
		int startIndex = (num - 1) * num2 + 1;
		int endIndex = num * num2;
		string str = "ROW_NUMBER() OVER (" + queryByOrder + ") as row,o.BuyerName,MoneyGoods,Deliver_TotalPrice,Invoice_TotalPrice,sio.* ";
		string strSQL = "select " + str + text + query;
		DataTable listPage = BLLInstance.BllClientByPage.GetListPage(strSQL, "row", startIndex, endIndex);
		this.rptList.DataSource = listPage;
		this.rptList.DataBind();
		decimal d = 0m;
		decimal d2 = 0m;
		decimal d3 = 0m;
		for (int i = 0; i < this.rptList.Items.Count; i++)
		{
			Label label = (Label)this.rptList.Items[i].FindControl("lblTotalFee");
			Label label2 = (Label)this.rptList.Items[i].FindControl("lblTotalReceiptMoney");
			Label label3 = (Label)this.rptList.Items[i].FindControl("lblTotalAlreadyReceiptMoney");
			if (label != null && label2 != null && label3 != null)
			{
				decimal d4 = Convert.ToDecimal(label.Text);
				d += d4;
				decimal d5 = Convert.ToDecimal(label2.Text);
				d2 += d5;
				decimal d6 = Convert.ToDecimal(label3.Text);
				d3 += d6;
			}
		}
		Label label4 = (Label)this.rptList.Controls[this.rptList.Controls.Count - 1].FindControl("lblFootTotalFee");
		Label label5 = (Label)this.rptList.Controls[this.rptList.Controls.Count - 1].FindControl("lblFootTotalReceiptMoney");
		Label label6 = (Label)this.rptList.Controls[this.rptList.Controls.Count - 1].FindControl("lblFootTotalAlreadyReceiptMoney");
		label4.Text = d.ToString();
		label5.Text = d2.ToString();
		label6.Text = d3.ToString();
		string url = this.beforeUrl + this.linkHead + "&pageindex";
		this.ltrPagePanel.Text = CurrPageLinks.instance.CurrPageLinkStr(num, num2, listCount, "条记录&nbsp;每页" + num2 + "条记录&nbsp;", url, true);
	}
	protected void Button_Command(object sender, CommandEventArgs e)
	{
		string comeUrl = this.beforeUrl + this.linkHead + "&pageindex";
		string commandName = e.CommandName;
		if (commandName == "DelInfo")
		{
			string text = e.CommandArgument.ToString();
			HoWave.Data.Model.StockInfoOrder model = BLLInstance.BllStockInfoOrder.GetModel(Convert.ToInt32(text));
			if (model != null)
			{
				DataTable dataTable = BLLInstance.BllAccountAoucher.GetList("StockOrderID=" + text).Tables[0];
				if (dataTable.Rows.Count > 0)
				{
					this.WriteErrMsg("", "库单中有凭证信息，不能删除！", comeUrl);
				}
				else
				{
					DataTable dataTable2 = BLLInstance.BllStockItemOrder.GetList("InfoOrderID=" + text).Tables[0];
					bool flag;
					for (int i = 0; i < dataTable2.Rows.Count; i++)
					{
						int itemOrderID = Convert.ToInt32(dataTable2.Rows[i]["ItemOrderID"].ToString());
						HoWave.Data.Model.StockItemOrder model2 = BLLInstance.BllStockItemOrder.GetModel(itemOrderID);
						if (model2 != null)
						{
							flag = BLLInstance.BllStockItemOrder.Delete(itemOrderID);
						}
					}
					flag = BLLInstance.BllStockInfoOrder.Delete(Convert.ToInt32(text));
					if (flag)
					{
						this.WriteSuccessMsg("", "删除信息成功！", comeUrl);
					}
				}
			}
		}
	}
	protected void btnSearch_Click(object sender, EventArgs e)
	{
		this.BindData();
	}
	private bool CheckInput()
	{
		bool result = true;
		if (!string.IsNullOrEmpty(this.txtReceiptEndDate.Text))
		{
			if (!PageValidate.IsDateTime(this.txtReceiptEndDate.Text))
			{
				MessageBox.Show(this, "收款期限格式错误！");
				result = false;
			}
		}
		return result;
	}
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (this.CheckInput())
		{
			if (this.InfoOrderID > 0)
			{
				HoWave.Data.Model.StockInfoOrder model = BLLInstance.BllStockInfoOrder.GetModel(this.InfoOrderID);
				if (model != null)
				{
					DateTime value = DateTime.Parse(this.txtReceiptEndDate.Text);
					string text = this.txtRemarks.Text;
					model.InfoOrderID = this.InfoOrderID;
					model.UpdateDate = new DateTime?(DateTime.Now);
					model.Inputer = this.txtInputer.Text;
					model.ReceiptEndDate = new DateTime?(value);
					model.Remark = text;
					bool flag = BLLInstance.BllStockInfoOrder.Update(model);
					if (flag)
					{
						this.WriteSuccessMsg("", "修改销售出库单信息成功！", "StockOrderSaleSearch.aspx");
					}
				}
			}
		}
	}
}
